package com.yy.community.modules.comment.controller;

import cn.hutool.json.JSONUtil;
import com.yy.community.common.base.Result;
import com.yy.community.common.utils.PageUtils;
import com.yy.community.common.validator.ValidatorUtils;
import com.yy.community.common.validator.group.AddGroup;
import com.yy.community.modules.comment.entity.Comment;
import com.yy.community.modules.comment.service.ICommentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 司马缸砸缸了
 * @since 2019-07-12
 */

@Api(value = "评论控制器", tags = "评论")
@Slf4j
@RestController
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private ICommentService commentService;

    @ApiOperation(value = "分页查询评论接口", notes = "条件，分页查询")
    @GetMapping("")
    public Result getPage(@ApiParam(value = "请求过滤参数") @RequestParam Map<String, Object> params) {
        log.info("分页查询所有评论接口，参数:{}", JSONUtil.toJsonStr(params));
        PageUtils page = commentService.queryPage(params);

        return Result.ok(page);
    }

    @ApiOperation(value = "条件查询评论接口", notes = "条件，不分页查询")
    @GetMapping("/list")
    public Result getList(Comment comment) {
        log.info("查询所有评论接口，参数:{}", JSONUtil.toJsonStr(comment));
        List<Comment> list = commentService.queryList(comment);

        return Result.ok(list);
    }

    @ApiOperation(value = "添加评论", notes = "添加评论")
    @PostMapping("/")
    public Result save(@ApiParam(value = "Comment实体类", required = true) @RequestBody Comment comment) {
        log.info("添加评论参数:{}", JSONUtil.toJsonStr(comment));
        ValidatorUtils.validateEntity(comment, AddGroup.class);
        commentService.save(comment);

        return Result.ok();
    }

    @ApiOperation(value = "删除评论", notes = "删除评论")
    @DeleteMapping("/{id}")
    public Result delete(@ApiParam(value = "ID主键", required = true) @PathVariable String id) {
        log.info("删除评论,ID:{}", id);
        commentService.removeById(id);

        return Result.ok();
    }

}
